package com.cheng.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cheng.dao.CourierDao;
import com.cheng.domain.base.Courier;
import com.cheng.service.base.CourierService;

@Service
@Transactional
public class CourierServiceImpl implements CourierService {
	@Autowired
	private CourierDao courierDao;

	@Override
	@RequiresPermissions("courier:add")
	public void addCourier(Courier courier) {
		courier.setDeltag('0');
		courierDao.save(courier);
	}

	@Override
	public Page<Courier> findAllCourierBypage(Pageable pageable,Specification<Courier> specification) {
		return courierDao.findAll(specification,pageable);
	}

	@Override
	@RequiresPermissions("courier:del")
	public void delCourier(String[] str) {
		for (String string : str) {
			int id = Integer.parseInt(string);
			courierDao.delCourier(id);
		}
	}

	@Override
	public void resumeCourier(String[] str) {
		for (String string : str) {
			int id = Integer.parseInt(string);
			courierDao.resumeCourier(id);
		}
	}

	@Override
	public List<Courier> findNOassiocationCourier() {
		Specification<Courier> specification = new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				Predicate p1 = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				Predicate p2 = cb.notEqual(root.get("deltag").as(Character.class),'1');
				return cb.and(p1,p2);
			}
		};
		return courierDao.findAll(specification);
	}
	
	
}
